package ar.edu.unicen.exa.oop.bbquery.model.forum;

import java.net.URI;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class that maps a post.
 * 
 * @author nicolas
 * @model
 * 
 */
@Entity
public class Post {

	@Transient
	private static Logger log = LoggerFactory.getLogger(Post.class);

	private String title;

	/*
	 * por ahora capturamos una string
	 */
	private String creator;

	// private User creator;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "post_id")
	private Integer id;

	@Temporal(TemporalType.TIME)
	private Date created_date;
	/* element de DOM ... */
	private String content;

	@OneToMany
	@JoinColumn(name = "parent_id")
	private List<Post> replies;

	private URI linkToPost;

	public Post() {
		this(0);
	}

	protected Post(Integer id) {
		log.info("Post ( {} : DUMMY ) Created ", title);
		this.setContent("");
		this.setCreated_date(new Date());
		this.setCreator("");
		this.setReplies(new LinkedList<Post>());
		this.setLinkToPost(null);
		this.setTitle("");
		this.setId(id);
	}

	protected Post(String title, String crtr, Date cr_date, String pcont,
			URI ltp) {
		log.info("Post ( {} ) Created ", title);
		this.setContent(pcont);
		this.setCreated_date(cr_date);
		this.setCreator(crtr);
		this.setReplies(new LinkedList<Post>());
		this.setLinkToPost(ltp);
		this.setTitle(title);
	}

	/*
	 * void setCreator(User creator) { this.creator = creator; }
	 * 
	 * User getCreator() { return creator; }
	 */

	public void setCreated_date(Date created_date) {
		this.created_date = created_date;
	}

	public Date getCreated_date() {
		return created_date;
	}

	public void setReplies(LinkedList<Post> replies) {
		this.replies = replies;
	}

	public List<Post> getReplies() {
		return replies;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getCreator() {
		return creator;
	}

	/**
	 * @param linkToPost
	 *            the linkToPost to set
	 */
	public void setLinkToPost(URI linkToPost) {
		this.linkToPost = linkToPost;
	}

	/**
	 * @return the linkToPost
	 */
	public URI getLinkToPost() {
		return linkToPost;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	public boolean addReply(Post t) {
		return this.replies.add(t);
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

}
